<template>
  <el-dialog
    :title="!dataForm.advertId ? '新增广告' : '修改广告'"
    :append-to-body="true"
    :close-on-click-modal="isShow"
    :visible.sync="visible"
    width="80%">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" label-width="80px">
      <el-form-item label="名称" prop="advertName">
        <el-input v-model="dataForm.advertName" placeholder="请输入名称"></el-input>
      </el-form-item>
      <el-form-item label="广告图片">
        <el-upload
          class="avatar-uploader"
          :action="url"
          :show-file-list="false"
          :on-success="handleAvatarSuccess"
          :before-upload="beforeAvatarUpload">
          <img v-if="dataForm.advertPicUrl" :src="dataForm.advertPicUrl" class="avatar">
          <i v-else class="el-icon-plus avatar-uploader-icon"></i>
        </el-upload>
      </el-form-item>
      <el-form-item label="链接地址" prop="advertLink">
        <el-input v-model="dataForm.advertLink" placeholder="请输入链接"></el-input>
      </el-form-item>
      <el-form-item label="是否弃用" prop="state">
        <el-radio-group v-model="dataForm.state">
          <el-radio :label="1">启用</el-radio>
          <el-radio :label="0">关闭</el-radio>
        </el-radio-group>
      </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" :disabled="dataListLoading" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        dataListLoading: false,
        isShow: false,
        visible: false,
        url: '',
        dataForm: {
          advertId: '',
          typeId: '',
          typeName: '',
          advertName: '',
          advertPicUrl: '',
          advertLink: '',
          state: 1
        },
        dataRule: {
          advertName: [
            { required: true, message: '名称不能为空', trigger: 'blur' }
          ],
          advertPicUrl: [
            { required: true, message: '请选择图片', trigger: 'blur' }
          ],
          advertLink: [
            { required: true, message: '广告链接不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      handleAvatarSuccess (res, file) {
        // this.dataForm.shopPicUrl = URL.createObjectURL(file.raw)
        if (res && res.code === 0) {
          // this.dataForm.advertPicUrl = res.url
          this.dataForm.advertPicUrl = this.openImg(res.url)
          console.log(this.dataForm)
        } else {
          this.$message.error(res.msg)
        }
      },
      openImg (url) {
        if (url) {
          url = this.$http.adornUrl(`/pig-file/center/file/openImg?url=${url}`)
          return url
        } else {
          return null
        }
      },
      beforeAvatarUpload (file) {
        const isJPG = file.type === 'image/jpeg' || file.type === 'image/png' || file.type === 'image/gif'
        const isLt2M = file.size / 1024 / 1024 < 2
        if (!isJPG) {
          this.$message.error('上传头像图片只能是 jpg,pgn,gif 格式!')
        }
        if (!isLt2M) {
          this.$message.error('上传头像图片大小不能超过 2MB!')
        }
        return isJPG && isLt2M
      },
      init (id, typeId, typeName) {
        this.dataForm.advertId = id || 0
        this.dataForm.typeId = typeId
        this.dataForm.typeName = typeName
        this.dataForm.advertPicUrl = ''
        this.url = this.$http.adornUrl(`/pig-file/center/file/upload`)
        this.dataListLoading = false
        this.visible = true
        this.isShow = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.advertId) {
            this.$http({
              url: this.$http.adornUrl(`/pig-center-biz/advert/ment/info`),
              method: 'get',
              params: this.$http.adornParams({
                'id': this.dataForm.advertId
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm.advertId = data.advert.advertId
                this.dataForm.typeId = data.advert.typeId
                this.dataForm.advertName = data.advert.advertName
                this.dataForm.advertPicUrl = data.advert.advertPicUrl
                this.dataForm.advertLink = data.advert.advertLink
                this.dataForm.state = data.advert.state
              }
            })
          }
        })
      },
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.dataListLoading = true
            this.$http({
              url: this.$http.adornUrl(`/pig-center-biz/advert/ment/${!this.dataForm.advertId ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'advertId': this.dataForm.advertId || undefined,
                'typeId': this.dataForm.typeId,
                'advertName': this.dataForm.advertName,
                'advertPicUrl': this.dataForm.advertPicUrl,
                'advertLink': this.dataForm.advertLink,
                'state': this.dataForm.state
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList', this.dataForm.typeId, this.dataForm.typeName)
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>

<style>
  avatar-uploader .el-upload {
    border: 1px dashed #d9d9d9;
    border-radius: 6px;
    cursor: pointer;
    position: relative;
    overflow: hidden;
  }
  .avatar-uploader .el-upload:hover {
    border-color: #409EFF;
  }
  .avatar-uploader-icon {
    font-size: 28px;
    color: #8c939d;
    width: 178px;
    height: 178px;
    line-height: 178px;
    text-align: center;
  }
  .avatar {
    width: 80px;
    height: 80px;
    display: block;
  }
</style>
